/*
 Name:		ESP32_Bluetooth_Serial_vs.ino
 Created:	2020/6/12 14:10:14
 Author:	XUE
*/

#include "BluetoothSerial.h"
#include "ELMduino.h"
#include "SSD1306.h"
SSD1306 display(0x3c, 21, 22);
BluetoothSerial SerialBT;
#define ELM_PORT   SerialBT
#define DEBUG_PORT Serial

String showString = "hello";
ELM327 myELM327;


uint32_t rpm = 0;


void setup()
{
#if LED_BUILTIN
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
#endif
    show();
    DEBUG_PORT.begin(115200);
    SerialBT.setPin("1234");
    ELM_PORT.begin("ArduHUD", true);

    if (!ELM_PORT.connect("OBDII"))
    {
        DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");
        while (1);
    }

    if (!myELM327.begin(ELM_PORT))
    {
        Serial.println("Couldn't connect to OBD scanner - Phase 2");
        while (1);
    }

    Serial.println("Connected to ELM327");
}


void loop()
{
    float tempRPM = myELM327.rpm();
    //车速
    int32_t kmh = myELM327.kph();
    if (myELM327.status == ELM_SUCCESS)
    {
        //转速
        // rpm = (uint32_t)tempRPM;
        // Serial.print("RPM: "); Serial.println(rpm);
        // showString = rpm;
        // show();
        // delay(100);
        //转换车速
        int32_t m_kmh = kmh;
        char cm_kmh[4];
        itoa(m_kmh, cm_kmh, 10);

        kmh = map(kmh, 0, 255, 0, 280);
        char c_kmh[4];
        itoa(kmh, c_kmh, 10);
        //c_kmh 为真正的车速
        Serial.print("车速:");
        Serial.print(c_kmh);
        //输出车速
        showString = c_kmh;
        show();
        delay(100);     
    }
    else
        printError();
}

void printError()
{
    Serial.print("Received: ");
    for (byte i = 0; i < myELM327.recBytes; i++)
        Serial.write(myELM327.payload[i]);
    Serial.println();

    if (myELM327.status == ELM_SUCCESS){
        Serial.println(F("\tELM_SUCCESS"));
        showString = "SUCCESS";
    }else if (myELM327.status == ELM_NO_RESPONSE){
        Serial.println(F("\tERROR: ELM_NO_RESPONSE"));
         showString = "NO_RESPONSE";
    }else if (myELM327.status == ELM_BUFFER_OVERFLOW){
        Serial.println(F("\tERROR: ELM_BUFFER_OVERFLOW"));
         showString = "BUFFER_OVERFLOW";
    }else if (myELM327.status == ELM_UNABLE_TO_CONNECT){
        Serial.println(F("\tERROR: ELM_UNABLE_TO_CONNECT"));
         showString = "UNABLE_TO_CONNECT";
    }else if (myELM327.status == ELM_NO_DATA){
        Serial.println(F("\tERROR: ELM_NO_DATA"));
         showString = "NO_DATA";
    }else if (myELM327.status == ELM_STOPPED){
        Serial.println(F("\tERROR: ELM_STOPPED"));
         showString = "STOPPED";
    }else if (myELM327.status == ELM_TIMEOUT){
        Serial.println(F("\tERROR: ELM_TIMEOUT"));
         showString = "TIMEOUT";
    }else if (myELM327.status == ELM_TIMEOUT){
        Serial.println(F("\tERROR: ELM_GENERAL_ERROR"));
         showString = "GENERAL_ERROR";
    }
    delay(100);
}
void show(){
         display.init();
          display.setFont(ArialMT_Plain_24);
          display.drawString(0, 20, showString);
          display.display();
}
void errorShow(){
         display.init();
          display.setFont(ArialMT_Plain_10);
          display.drawString(0, 20, showString);
          display.display();
}
